package com.qidian.buy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qidian.buy.entity.Category;
import com.qidian.buy.mapper.CategoryMapper;
import com.qidian.buy.service.ICategoryService;
import com.qidian.buy.util.Const;
import com.qidian.buy.util.pojo.LayuiTableDTO;
import com.qidian.buy.util.pojo.QDResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author 郭浩
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Override
    public QDResult save(Category category) {
        category.setCreated(new Date());
        category.setStatus(Const.STATUS_NORMAL);
        category.setUpdated(new Date());
        return categoryMapper.insert(category)==1?QDResult.ok(category):QDResult.fal();
    }

    @Override
    public QDResult update(Category category) {
        category.setUpdated(new Date());
        return categoryMapper.updateById(category)==1?QDResult.ok(category):QDResult.fal();
    }

    @Override
    public QDResult<Category> get(Long id) {
        return QDResult.ok(categoryMapper.selectById(id));
    }

    @Override
    public LayuiTableDTO<Category> query(int page, int limit, Category category) {
        LayuiTableDTO<Category> dto = new LayuiTableDTO<>();
        dto.setData(categoryMapper.queryByPage((page-1)*limit,limit,category));
        dto.setCount(categoryMapper.queryCount(category));
        return dto;
    }

    public QDResult queryAllFirstLevel(){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",'0');
        List<Category> list = categoryMapper.selectList(wrapper);
        return QDResult.ok(list);
    }

    @Override
    public QDResult queryByParentId(Long parentId) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        return QDResult.ok(categoryMapper.selectList(wrapper));
    }
}
